//index.js
//获取应用实例
const app = getApp();
const configs = require('../../configs/config');
const helper = require('../../utils/hepler');
Page({
  data: {
    imgUrls: [
      'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    previousMargin:20,
    nextMargin:20,
  },
  onLoad: function () {

  },
  doQode(){ 
    var that = this;
    // 只允许从相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res.result);
        that.getProductInfo(res.result)
      }
    })
  },
  getProductInfo(qcode){ 
    //加载中
    wx.showLoading({
      title:'加载中...',
    })
    //get请求数据
    wx.request({
      url: configs.apiUrl +'api/getProduct', 
      data: {
        qcode
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading();
        //这里直接做判断处理扫码出现不完整的情况
        if(res.data.result.length > 0){
          helper.addCart(res.data.result[0]);
          wx.navigateTo({
            url: '../cart/cart',
          })
        }else{
          wx.showToast({
            title:'此商品不存在',
            icon:"success",
            duration:3000
          })
        }
       
      }
    })
  }
})
